//
//  AIGirlDiamondItem.swift
//  AIGirl
//
//  Created by Rae on 2024/1/2.
//

import UIKit

class AIGirlDiamondItem: UICollectionViewCell {
    
    private let backView = c_make_ex(UIView.self)
        .corner(40, corners: [.layerMaxXMinYCorner,.layerMinXMaxYCorner]).end()
    private let rectView = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.13)).corner(15, corners: [.layerMinXMinYCorner,.layerMaxXMaxYCorner]).end()
    private let diamondImg = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let countLabel = c_make_ex(UILabel.self)
        .font(20.font_bold).alignment(.center).end()
    private let popularImg = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_popular").mode(.scaleAspectFill).end()
    private lazy var bugButton = c_make_ex(UIButton.self)
        .text("$ 49.99").color(.white).font(16.font_bold)
        .bgColor("#7F58FF").corner(15).end()
    
    var model: AIGirlHomeItemModel?{
        didSet{
            guard let md = model else { return }
            bugButton.setTitle("$ " + md.price, for: .normal)
            diamondImg.image = "AIGirl_Home_Diamond_\(md.index)".toImage
            var coin = md.originalCoin
            if md.addCoin != "0" {
                coin = coin + "+" + md.addCoin
            }
            let coinCount = md.coin.count
            let attributedString = NSMutableAttributedString(string: coin)
            let textRange1 = NSRange(location: 0, length: coinCount)
            let textRange2 = NSRange(location: coinCount, length: coin.count - coinCount)
            attributedString.addAttribute(.foregroundColor, value: UIColor.white, range: textRange1)
            attributedString.addAttribute(.foregroundColor, value: "#FED43D".toColor, range: textRange2)
            countLabel.attributedText = attributedString
            if md.price == "9.99" {
                popularImg.isHidden = false
            }else{
                popularImg.isHidden = true
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .clear

        contentView.subviews {
            backView.subviews{
                rectView.subviews{
                    diamondImg
                    countLabel
                    bugButton
                    popularImg
                }
            }
            
        }

        backView.fillContainer()
        rectView.fillContainer()
        diamondImg.top(0).leading(6).trailing(6).height(90)
        countLabel.leading(5).trailing(5)
        countLabel.Top == diamondImg.Bottom + 3
        bugButton.width(110).height(30).bottom(18).centerHorizontally()
        popularImg.leading(0).top(0).width(86).height(62)
        
        bugButton.isUserInteractionEnabled = false
    }
    

    override func updateConstraintsIfNeeded() {
        super.updateConstraintsIfNeeded()
        layoutIfNeeded()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
